{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5158d836-69f5-4c4c-b5a0-19a7decb99fb",
   "metadata": {},
   "source": [
    "# 逻辑回归 多变量梯度下降"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "59874552-2e7a-4a76-a231-c8b5efc326c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# sigmoid函数和初始化数据\n",
    "def sigmoid(z):\n",
    "    return 1 / (1 + np.exp(-z))\n",
    "\n",
    "def init_data():\n",
    "    data = np.loadtxt('data.csv')\n",
    "#     print(data)\n",
    "    dataMatIn = data[:, 0:-1]\n",
    "#     print(dataMatIn)\n",
    "    classLabels = data[:, -1]\n",
    "#     print(classLabels)\n",
    "    dataMatIn = np.insert(dataMatIn, 0, 1, axis=1)  #特征数据集，添加1是构造常数项x0\n",
    "    return dataMatIn, classLabels\n",
    "\n",
    "dataMatIn, classLabels = init_data()\n",
    "# print(dataMatIn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "77a73a1b-0ad8-46a0-9a6f-356fa2db248a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "# 绘图\n",
    "def plotBestFIt(weights):\n",
    "    dataMatIn, classLabels = init_data()\n",
    "    n = np.shape(dataMatIn)[0]\n",
    "    xcord1 = []\n",
    "    ycord1 = []\n",
    "    xcord2 = []\n",
    "    ycord2 = []\n",
    "    for i in range(n):\n",
    "        if classLabels[i] == 1:\n",
    "            xcord1.append(dataMatIn[i][1])\n",
    "            ycord1.append(dataMatIn[i][2])\n",
    "        else:\n",
    "            xcord2.append(dataMatIn[i][1])\n",
    "            ycord2.append(dataMatIn[i][2])\n",
    "    fig = plt.figure()\n",
    "    ax = fig.add_subplot(111)\n",
    "    ax.scatter(xcord1, ycord1,s=30, c='red', marker='s')\n",
    "    ax.scatter(xcord2, ycord2, s=30, c='green')\n",
    "    x = np.arange(-3, 3, 0.1)\n",
    "    y = (-weights[0, 0] - weights[1, 0] * x) / weights[2, 0]  #matix\n",
    "    ax.plot(x, y)\n",
    "    plt.xlabel('X1')\n",
    "    plt.ylabel('X2')\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "dc4c8dc9-7278-4696-8859-2b6354fcfb8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "m: 100 n: 3\n",
      "weights: [[1.]\n",
      " [1.]\n",
      " [1.]]\n",
      "[[ 4.12414349]\n",
      " [ 0.48007329]\n",
      " [-0.6168482 ]]\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEGCAYAAAB7DNKzAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAjcklEQVR4nO3de3SddZ3v8fc3aVqgzS6XJiXphXLpKU2xRclK9eCNQZnKMOKMyoIzaw6MjqWukdE5x9OjMkdGnFlq1dFZoLZw4Oi48MJRQZYWpCproRwpbZnWQku1FEp6oU2BNimFtmm+54/9JE129n6yd7L3c8vntVZWs5/9JP3m9nz37/v9/X6PuTsiIiKl1MUdgIiIJJsShYiIhFKiEBGRUEoUIiISSolCRERCTYg7gFqYNm2az5kzJ+4wRERSY8OGDQfcvanYc5lMFHPmzGH9+vVxhyEikhpmtrPUcyo9iYhIKCUKEREJVfNEYWZ3m9l+M3tq0LF/MrPdZrYxeLuyxMcuMbNtZrbdzD5V61hFRGS4KEYU3waWFDn+NXe/OHhbXfikmdUD3wDeA7QB15lZW00jFRGRYWqeKNz9UeDlUXxoB7Dd3Xe4+zHgB8DVVQ1ORERGFGeP4mNm9vugNHVGkednAJ2DHu8KjhVlZkvNbL2Zre/q6qp2rCKp03mok5tW30THnR3ctPomOg91jvxBIkXElSi+BZwPXAzsBb461k/o7ne4e7u7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cUsNwyjZS3GZ2A3AVcLknpOlVwfc66XYDswY9nhkckxoJ6vw/Bu5x95/EHU8l3P2gmT0CLAGqNpFAI4qEMLO5gx5eDTwTVyyVMLMlwHLgve5+JO54MmgdMNfMzjWzicC1wAMxx5RZQWP4LmCru/9r3PGUw8ya+mcbmtmp5Cc+VPX6oVlPCWFmPwbmkZ+NsxNY5u6Jf+VoZtuBScBLwaHHkz5by8z+ArgNaAIOAhvd/U9jDSqEmV0JfB2oB+5293+JN6KRmdn3gXeS3/Z6H3CLu98Va1BlMLO3Ar8BNpP/WwT4jLuvji+qcGa2EPgO+d+POuBed7+1qv+HEoWIiIRR6UlEREIpUYiISCglChERCaVEISIioZQoREQklBKFSA0Eu5A+Z2ZnBo/PCB7PMbOHzOygmf0s7jhFyqFEIVID7t4JfAv4YnDoi8Ad7v488GXgr2MKTaRiShQitfM14M1m9gngrcBXANz9V0BPjHGJVER7PYnUiLsfN7P/ATwEXOHux+OOSWQ0NKIQqa33AHuB1Ny8R6SQEoVIjZjZxeQ3aHsz8A/B3dNEUkeJQqQGgl1Iv0X+fgYvkG9gfyXeqERGR4lCpDY+Arzg7muCx98E5pvZO8zsN8D/BS43s11mltida0VAu8eKiMgINKIQEZFQShQiIhJKiUJEREIpUYiISCglChERCaVEISIioZQoREQk1P8H7u6F3Y/s7mcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 梯度下降\n",
    "def grad_descent(dataMatIn, classLabels):\n",
    "    dataMatrix = np.mat(dataMatIn)  #(m,n)\n",
    "#     print(dataMatrix)\n",
    "    labelMat = np.mat(classLabels).transpose()\n",
    "#     labelMat = np.mat(classLabels)\n",
    "#     print(labelMat)\n",
    "    m, n = np.shape(dataMatrix)\n",
    "    print('m:', m, 'n:',n)\n",
    "    weights = np.ones((n, 1))  #初始化回归系数（n, 1)\n",
    "    print('weights:', weights) \n",
    "    alpha = 0.001 #步长\n",
    "    maxCycle = 500  #最大循环次数\n",
    "\n",
    "    for i in range(maxCycle):\n",
    "        h = sigmoid(dataMatrix * weights)  #sigmoid 函数\n",
    "        weights = weights + alpha * dataMatrix.transpose() * (labelMat - h)  #梯度\n",
    "    return weights\n",
    "\n",
    "r = grad_descent(dataMatIn, classLabels)\n",
    "print(r)\n",
    "plotBestFIt(r)\n",
    "# r = stoc_grad_ascent(dataMatIn, classLabels)\n",
    "# r = stoc_grad_ascent_one(dataMatIn, classLabels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "89779dd9-cb7e-4969-9ec6-2c3bcddd5ca7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.01702007  0.85914348 -0.36579921]\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "too many indices for array: array is 1-dimensional, but 2 were indexed",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_692/817238255.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstoc_grad_ascent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataMatIn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclassLabels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 14\u001b[0;31m \u001b[0mplotBestFIt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/tmp/ipykernel_692/2329349473.py\u001b[0m in \u001b[0;36mplotBestFIt\u001b[0;34m(weights)\u001b[0m\n\u001b[1;32m     61\u001b[0m     \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscatter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxcord2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mycord2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'green'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     62\u001b[0m     \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m     \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m  \u001b[0;31m#matix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     64\u001b[0m     \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     65\u001b[0m     \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mxlabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'X1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIndexError\u001b[0m: too many indices for array: array is 1-dimensional, but 2 were indexed"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAbF0lEQVR4nO3df5Bd5X3f8fdHv0iBleyGBYMQllszxDJBJOysmsGO7cEmMkPBqR0GptOKmEbIY6tOpxnVCa2dkmSmkd0kE5RagkLtdGRsahvMJMKGJJ7BZkBoRSQk88NVKHQlbFjACBGlwKJv/9i78uru/X3PPec5535eM3f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GREXMnOEPS4p0zSaL3HYg4j4YIerbgd2AJ8bYHM61q7dkq4FLgcuiUQGd7r4rlN3CFgx5/HZtWU2ILU8+DeA7RHxzaLb042IeFnSd4G1QGYD5u7hZ0zSuXMeXgk8UVRbuiFpLbAJuCIijhbdngraBZwr6R2SlgBXA3cX3KbKqg2A3go8HhF/VHR7OiFpdLY6TtI/YmaAP9P44SqdjEn6BnAeM9UjzwAbIiL5npykA8BJwIu1RQ+lXl0k6VeBm4BR4GVgT0T8SqGNakHSZcCfAAuB2yLiD4ptUXuSbgfez8x0vc8Bn4uIWwttVAckvQf4HrCPmf9FgN+JiB3Ftao1SRcAX2bm72MBcEdE3JjpNhzwzcyGg1M6ZmZDwgHfzGxIOOCbmQ0JB3wzsyHhgG9mNiQc8M3MhoQDvpnZkPj/ojtfuZKPWA4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 梯度上升\n",
    "def stoc_grad_ascent(dataMatIn, classLabels):\n",
    "    m, n = np.shape(dataMatIn)\n",
    "    alpha = 0.01\n",
    "    weights = np.ones(n)\n",
    "    for i in range(m):\n",
    "        h = sigmoid(sum(dataMatIn[i] * weights))  #数值计算\n",
    "        error = classLabels[i] - h\n",
    "        weights = weights + alpha * error * dataMatIn[i]\n",
    "    return weights\n",
    "\n",
    "r = stoc_grad_ascent(dataMatIn, classLabels)\n",
    "print(r)\n",
    "plotBestFIt(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "8c699653-d96b-4fe5-abb1-50a0ecbd4377",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[15.54740137  1.23272371 -2.18652802]\n"
     ]
    },
    {
     "ename": "IndexError",
     "evalue": "too many indices for array: array is 1-dimensional, but 2 were indexed",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_692/4047881156.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0mr\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mstoc_grad_ascent_one\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataMatIn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mclassLabels\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mplotBestFIt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/tmp/ipykernel_692/2329349473.py\u001b[0m in \u001b[0;36mplotBestFIt\u001b[0;34m(weights)\u001b[0m\n\u001b[1;32m     61\u001b[0m     \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscatter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mxcord2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mycord2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m30\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'green'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     62\u001b[0m     \u001b[0mx\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0.1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 63\u001b[0;31m     \u001b[0my\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0mweights\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m  \u001b[0;31m#matix\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     64\u001b[0m     \u001b[0max\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mplot\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     65\u001b[0m     \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mxlabel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'X1'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mIndexError\u001b[0m: too many indices for array: array is 1-dimensional, but 2 were indexed"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXwAAAD4CAYAAADvsV2wAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAbF0lEQVR4nO3df5Bd5X3f8fdHv0iBleyGBYMQllszxDJBJOysmsGO7cEmMkPBqR0GptOKmEbIY6tOpxnVCa2dkmSmkd0kE5RagkLtdGRsahvMJMKGJJ7BZkBoRSQk88NVKHQlbFjACBGlwKJv/9i78uru/X3PPec5535eM3f23nPPvee5d3e/5znf53ueo4jAzMyqb0HRDTAzs3w44JuZDQkHfDOzIeGAb2Y2JBzwzcyGxKKiG9DKaaedFitXriy6GWZmpbF79+4XImK00XNJB/yVK1cyMTFRdDPMzEpD0jPNnnNKx8xsSDjgm5kNiY4DvqTbJD0vaf+cZb8r6ZCkPbXbZU1eu1bSk5IOSPpMFg03M7PudNPD/xKwtsHyP46IC2u3HfVPSloI/BnwYWAVcI2kVb001szMetdxwI+I+4GXetjGOHAgIp6KiNeBrwJX9vA+ZmbWhyxy+J+S9Ggt5fPWBs8vBybnPD5YW9aQpPWSJiRNTE1NZdA8s3KbPDzJxh0bGb9lnI07NjJ5eLL9i8wa6DfgfxH4p8CFwI+A/9pvgyLi5ogYi4ix0dGGpaRmQ2Py8CSrt65m2+5t7Hp2F9t2b2P11tUO+taTvgJ+RDwXEW9GxDHgFmbSN/UOASvmPD67tszM2tj8wGZeff1V3jj2BgBvHHuDV19/lc0PbC64ZVZGfQV8SWfOefirwP4Gq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q9HlJT9TafqektxTdpnYk/ZqkH0g6Jin50jtJayU9KemApM8U3Z5OSLpN0vOS9hfdlm5IWiHpu5Ieq/2NfLroNrUj6WckPSxpb63N/znzbTiHny1JSyPildr9fwusiogNBTerLUmXAn8TEdOS/hAgIv5Dwc1qSdK7gGPANuC3ImKi4CY1JWkh8EPgQ8BBYBdwTUQ8VmjD2pD0y8CrwJ9HxPlFt6dTks4EzoyIRySNALuBj6T8fUsScEpEvCppMfB94NMR8VBW23APP2Ozwb7mFKAUe9SIuDcipmsPHwLOLrI9nYiIxyPiyaLb0aFx4EBEPBURrwNfBa4suE1tRcT9wEtFt6NbEfGjiHikdv8I8DiwvNhWtRYzXq09XFy7ZRo/HPAHQNIfSJoE/iXw2aLb04OPA/cU3YiKWQ5Mznl8kMQDUFVIWgn8ArCz4Ka0JWmhpD3A88B9EZFpmx3weyDpryTtb3C7EiAiboiIFcB24FPFtvan2rW7ts4NwDQzbS9cJ202a0bSqcA3gN+sO/pOUkS8GREXMnOEPS4p0zSaL3HYg4j4YIerbgd2AJ8bYHM61q7dkq4FLgcuiUQGd7r4rlN3CFgx5/HZtWU2ILU8+DeA7RHxzaLb042IeFnSd4G1QGYD5u7hZ0zSuXMeXgk8UVRbuiFpLbAJuCIijhbdngraBZwr6R2SlgBXA3cX3KbKqg2A3go8HhF/VHR7OiFpdLY6TtI/YmaAP9P44SqdjEn6BnAeM9UjzwAbIiL5npykA8BJwIu1RQ+lXl0k6VeBm4BR4GVgT0T8SqGNakHSZcCfAAuB2yLiD4ptUXuSbgfez8x0vc8Bn4uIWwttVAckvQf4HrCPmf9FgN+JiB3Ftao1SRcAX2bm72MBcEdE3JjpNhzwzcyGg1M6ZmZDwgHfzGxIOOCbmQ0JB3wzsyHhgG9mNiQc8M3MhoQDvpnZkPj/ojtfuZKPWA4AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 梯度上升改进\n",
    "def stoc_grad_ascent_one(dataMatIn, classLabels, numIter=150):\n",
    "    m, n = np.shape(dataMatIn)\n",
    "    weights = np.ones(n)\n",
    "    for j in range(numIter):\n",
    "        dataIndex = list(range(m))\n",
    "        for i in range(m):\n",
    "            alpha = 4 / (1 + i + j) + 0.01 #保证多次迭代后新数据仍然有影响力\n",
    "            randIndex = int(np.random.uniform(0, len(dataIndex)))\n",
    "            h = sigmoid(sum(dataMatIn[i] * weights))  # 数值计算\n",
    "            error = classLabels[i] - h\n",
    "            weights = weights + alpha * error * dataMatIn[i]\n",
    "            del(dataIndex[randIndex])\n",
    "    return weights\n",
    "\n",
    "r = stoc_grad_ascent_one(dataMatIn, classLabels)\n",
    "print(r)\n",
    "plotBestFIt(r)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
